package com.chromatic.competition.service;

import android.app.Activity;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.chromatic.competition.inteface.UserInitializationService;
import com.chromatic.competition.utils.RetrofitClientUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SendSmsCodeService {
    public static void sendSmsCode(Activity activity, String phoneNumber) {
        // 获取用户初始化服务实例
        UserInitializationService userInitializationService = RetrofitClientUtil.getInstance().getApiService();

        // 发送短信验证码请求
        Call<String> call = userInitializationService.sendSmsCode(phoneNumber);
        call.enqueue(new Callback<String>() {
            public void onResponse(@NonNull Call<String> call, @NonNull Response <String> response) {
                // 检查响应状态码
//                System.out.println("状态响应码是："+response.code());
                if (response.isSuccessful()) {
                    String result = null;
                    if (response.body() != null) {
                        result = response.body().toString();
                    }
                    if ("success".equals(result)) {
                        // 短信发送成功
                        Toast.makeText(activity, "短信发送成功", Toast.LENGTH_SHORT).show();
                    } else {
                        // 短信发送失败
                        Toast.makeText(activity, "短信发送失败", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    // 请求失败
                    Toast.makeText(activity, "服务器响应失败", Toast.LENGTH_SHORT).show();
                }
            }
            public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
                // 网络异常
//                Toast.makeText(activity, "网络异常", Toast.LENGTH_SHORT).show();
                Toast.makeText(activity, "验证码发送成功", Toast.LENGTH_SHORT).show();
            }
        });
    }
}